package org.cuijq.thinking.in.spring.dependency.injection;

import org.cuijq.thinking.in.spring.ioc.domain.SuperUser;
import org.cuijq.thinking.in.spring.ioc.domain.User;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @author by CuiJQ
 * @Classname XmlDepencySetterInjectionDemo
 * @Description XmlDepencySetterInjectionDemo Description
 * @Date 2021/6/29 9:04
 */
public class ApiDepencySetterInjectionDemo {

  public static void main(String[] args) {
    //创建BeanFactory 容器
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    //注册
    BeanDefinition userHolderBeanDefiniton = createUserHolderBeanDefiniton();
    applicationContext.registerBeanDefinition("userHolder",userHolderBeanDefiniton);

    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);
    String xmlResourcePath = "classpath:/META-INF/dependency-lookuo-context.xml";
    beanDefinitionReader.loadBeanDefinitions(xmlResourcePath);

    applicationContext.refresh();

    UserHolder userHolder = applicationContext.getBean(UserHolder.class);
    System.out.println(userHolder);

    applicationContext.close();
  }

  private static BeanDefinition createUserHolderBeanDefiniton() {
    BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(UserHolder.class);
    definitionBuilder.addPropertyReference("user", "superUser");
    return definitionBuilder.getBeanDefinition();
  }

//  @Bean
//  public UserHolder userHolder(User user){
////    return new UserHolder(user);
//    UserHolder userHolder = new UserHolder();
//    userHolder.setUser(user);
//    return userHolder;
//  }
}
